3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace Wikimedia\Rdbms
;
23 use InvalidArgumentException
;
26 * Class to handle database/prefix specification for IDatabase domains
28 class DatabaseDomain
{
29 /** @var string|null */
31 /** @var string|null */
36 /** @var string Cache of convertToString() */
37 private $equivalentString;
40 * @param string|null $database Database name
41 * @param string|null $schema Schema name
42 * @param string $prefix Table prefix
44 public function __construct( $database, $schema, $prefix ) {
45 if ( $database !== null && ( !is_string( $database ) ||
$database === '' ) ) {
46 throw new InvalidArgumentException( 'Database must be null or a non-empty string.' );
48 $this->database
= $database;
49 if ( $schema !== null && ( !is_string( $schema ) ||
$schema === '' ) ) {
50 throw new InvalidArgumentException( 'Schema must be null or a non-empty string.' );
51 } elseif ( $database === null && $schema !== null ) {
52 throw new InvalidArgumentException( 'Schema must be null if database is null.' );
54 $this->schema
= $schema;
55 if ( !is_string( $prefix ) ) {
56 throw new InvalidArgumentException( "Prefix must be a string." );
58 $this->prefix
= $prefix;
62 * @param DatabaseDomain|string $domain Result of DatabaseDomain::toString()
63 * @return DatabaseDomain
65 public static function newFromId( $domain ) {
66 if ( $domain instanceof self
) {
70 $parts = array_map( [ __CLASS__
, 'decode' ], explode( '-', $domain ) );
75 if ( count( $parts ) == 1 ) {
76 $database = $parts[0];
77 } elseif ( count( $parts ) == 2 ) {
78 list( $database, $prefix ) = $parts;
79 } elseif ( count( $parts ) == 3 ) {
80 list( $database, $schema, $prefix ) = $parts;
82 throw new InvalidArgumentException( "Domain '$domain' has too few or too many parts." );
85 if ( $database === '' ) {
89 if ( $schema === '' ) {
93 $instance = new self( $database, $schema, $prefix );
94 $instance->equivalentString
= (string)$domain;
100 * @return DatabaseDomain
102 public static function newUnspecified() {
103 return new self( null, null, '' );
107 * @param DatabaseDomain|string $other
108 * @return bool Whether the domain instances are the same by value
110 public function equals( $other ) {
111 if ( $other instanceof self
) {
113 $this->database
=== $other->database
&&
114 $this->schema
=== $other->schema
&&
115 $this->prefix
=== $other->prefix
119 return ( $this->getId() === $other );
123 * Check whether the domain $other meets the specifications of this domain
125 * If this instance has a null database specifier, then $other can have any database
126 * specifier, including null. This is likewise true if the schema specifier is null.
127 * This is not transitive like equals() since a domain that explicitly wants a certain
128 * database or schema cannot be satisfied by one of another (nor null). If the prefix
129 * is empty and the DB and schema are both null, then the entire domain is considered
130 * unspecified, and any prefix of $other is considered compatible.
132 * @param DatabaseDomain|string $other
136 public function isCompatible( $other ) {
137 if ( $this->isUnspecified() ) {
138 return true; // even the prefix doesn't matter
141 $other = self
::newFromId( $other );
144 ( $this->database
=== $other->database ||
$this->database
=== null ) &&
145 ( $this->schema
=== $other->schema ||
$this->schema
=== null ) &&
146 $this->prefix
=== $other->prefix
154 public function isUnspecified() {
156 $this->database
=== null && $this->schema
=== null && $this->prefix
=== ''
161 * @return string|null Database name
163 public function getDatabase() {
164 return $this->database
;
168 * @return string|null Database schema
170 public function getSchema() {
171 return $this->schema
;
175 * @return string Table prefix
177 public function getTablePrefix() {
178 return $this->prefix
;
184 public function getId() {
185 if ( $this->equivalentString
=== null ) {
186 $this->equivalentString
= $this->convertToString();
189 return $this->equivalentString
;
195 private function convertToString() {
196 $parts = [ (string)$this->database
];
197 if ( $this->schema
!== null ) {
198 $parts[] = $this->schema
;
200 if ( $this->prefix
!= '' ||
$this->schema
!== null ) {
201 // If there is a schema, then we need the prefix to disambiguate.
202 // For engines like Postgres that use schemas, this awkwardness is hopefully
203 // avoided since it is easy to have one DB per server (to avoid having many users)
204 // and use schema/prefix to have wiki farms. For example, a domain schemes could be
205 // wiki-<project>-<language>, e.g. "wiki-fitness-es"/"wiki-sports-fr"/"wiki-news-en".
206 $parts[] = $this->prefix
;
209 return implode( '-', array_map( [ __CLASS__
, 'encode' ], $parts ) );
212 private static function encode( $decoded ) {
215 $length = strlen( $decoded );
216 for ( $i = 0; $i < $length; ++
$i ) {
217 $char = $decoded[$i];
218 if ( $char === '-' ) {
220 } elseif ( $char === '?' ) {
230 private static function decode( $encoded ) {
233 $length = strlen( $encoded );
234 for ( $i = 0; $i < $length; ++
$i ) {
235 $char = $encoded[$i];
236 if ( $char === '?' ) {
237 $nextChar = $encoded[$i +
1] ??
null;
238 if ( $nextChar === 'h' ) {
241 } elseif ( $nextChar === '?' ) {
258 function __toString() {
259 return $this->getId();